﻿using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using System.Collections.ObjectModel;

namespace WinUITest.Pages
{
    [Menu("绑定父级")]
    public sealed partial class BindParentPage : Page
    {
        public BindParentPageViewModel ViewModel { get; } = new();
        public BindParentPage()
        {
            InitializeComponent();
            ViewModel.Tag = "Parent Value";
            for (int i = 0; i < 10; i++)
            {
                ViewModel.Children.Add(i);
            }
        }
        private void TagLabl_Loaded(object sender, RoutedEventArgs e)
        {
            if (sender is not TextBlock textBlock) return;
            textBlock.SetBinding(TextBlock.TextProperty, new Binding()
            {
                Source = ViewModel,
                Path = new PropertyPath("Tag"),
                Mode = BindingMode.OneWay
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ViewModel.Tag = ViewModel.Tag == "Parent Value" ? "Parent Value Changed" : "Parent Value";
        }
    }
    public partial class BindParentPageViewModel : ObservableObject
    {
        [ObservableProperty]
        public partial string Tag { get; set; } = string.Empty;

        [ObservableProperty]
        public partial ObservableCollection<int> Children { get; set; } = [];
    }
}
